export default function({ route, req, res, store, next, redirect }) {
  if (!req || !process.server || Object.keys(req).includes("headers") == -1) {
    return;
  }
  const { path,fullPath } = route;
  let safe_list = ["/binding", "/pay-result"]; //不进行设备判断跳转的路由
  //设置是否在手机设备
  let is_mobile = /Android|webOS|iPhone|iPod|iPad|BlackBerry|SymbianOS|Windows Phone|NOKIA|SymbianOS/i.test(
    req.headers["user-agent"]
  );
  store.commit("SET_IS_MOBILE", is_mobile);

  var ua = req.headers["user-agent"];
  
  //设置是否在ios环境中
  var isiOS = !!ua.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
  store.commit("SET_IS_IOS", isiOS);
  
  //设置是否在微信环境
  if (ua.toLowerCase().match(/MicroMessenger/i) == "micromessenger") {
    store.commit("SET_IS_WECHAT", true);
  } else {
    store.commit("SET_IS_WECHAT", false);
  }
  
  //路由跳转
  if (safe_list.indexOf(path) !== -1) {
    return;
  }
  // if (is_mobile) {
  //   if (!/\/m/i.test(path)) {
  //     redirect({ path: `/m${path}` });
  //   }
  //   return;
  // } else {
  //   if (/\/m/i.test(path)) {
  //     redirect({ path: path.split(/\/m/)[1] });
  //   }
  //   return;
  // }
  if (is_mobile) {
    // console.log("你是谁："+store.user_type);
    if (!/\/m/i.test(fullPath)) {
      redirect({ path: `/m${fullPath}` });
    }
    return;
  } else {
    if (/\/m/i.test(fullPath)) {
      redirect({ path: fullPath.split(/\/m/)[1] });
    }
    return;
  }
}
